package com.example.demo.controller;

//在controller层不要出现domain实体doc

import com.example.demo.req.DocQueryReq;
import com.example.demo.req.DocSaveReq;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.DocQueryResp;
import com.example.demo.resp.PageResp;
import com.example.demo.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {


    @Resource
    private DocService docService;

    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId){
        CommonResp<List<DocQueryResp>> resp=new CommonResp<>();
        List <DocQueryResp> list=docService.all(ebookId);
        resp.setContent(list);
        return resp;
    }

        @GetMapping("/list")
        public CommonResp list(@Valid DocQueryReq req){
            CommonResp<PageResp<DocQueryResp>> resp=new CommonResp<>();
            PageResp<DocQueryResp> list=docService.list(req);
            resp.setContent(list);
            return resp;
        }

        /**
         * @RequestBody
         *   对应jsaon方式的post请求
         *
         *   ptrp  form表单就不用注解
         * */
    @PostMapping("/save")
    public CommonResp save(@RequestBody DocSaveReq req){
        CommonResp resp=new CommonResp<>();
       docService.save(req);

        return resp;
    }

    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr){
        CommonResp resp=new CommonResp<>();
        List<String> list=Arrays.asList(idsStr.split(","));
        docService.delete(list);

        return resp;
    }


    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id){
        CommonResp<String> resp=new CommonResp<>();
        String content=docService.findContent(id);
        resp.setContent(content);
        return resp;
    }

    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id) {
        CommonResp commonResp = new CommonResp();
        docService.vote(id);
        return commonResp;
    }
}
